package leetcode_day._2022._202201._1120;

import general_class.ListNode;

import java.util.*;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/1/16 20:53
 * 链表随机节点
 * 算法：抽样
 */
public class _16_382 {

    // 786
    public int[] kthSmallestPrimeFraction(int[] arr, int k) {
        int m = arr.length;
        if (k > m * m) k = m * m;
        PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(a -> arr[a[0]] / arr[a[1]]));
        for (int i = 0; i < m; i++) queue.offer(new int[]{arr[i], arr[m - 1 - i]});
        while (k-- > 1 && !queue.isEmpty()) {
            int[] poll = queue.poll();
            if (poll[1] - 1 >= 0) queue.offer(new int[]{poll[0], poll[1] - 1});
        }
        return queue.poll();
    }

    class Solution {
        List<Integer> list;
        Random random = new Random();

        public Solution(ListNode head) {
            list = new ArrayList<>();
            while (head != null) {
                list.add(head.val);
                head = head.next;
            }
        }

        public int getRandom() {
            return list.get(random.nextInt(list.size()));
        }
    }

}
